package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.util.PropertyUtil;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.SysReportMasterDao;
import com.ccp.dev.qbdms.model.SysReportMaster;
import com.ccp.dev.qbdms.model.SysReportSub;
import com.ccp.dev.qbdms.model.WQbdQualityAnalysisReport;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.system.model.SysOrg;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SysReportMasterService {
    @Resource
    private SysReportMasterDao sysReportMasterDao;
    @Resource
    private SysReportSubService sysReportSubService;
    @Resource
    private WQbdQualityAnalysisReportService wQbdQualityAnalysisReportService;
    @Resource
    private ExtendSysOrgService extendSysOrgService;

    /**0
     * 添加上报数据信息
     * @param sysReportMaster 上报主表对象
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveReportMaster(SysReportMaster sysReportMaster,String dataId){

        if(BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.PLANTLEVEL){
            //三级单位启动流程后修改状态为报院审批中
            sysReportMasterDao.updateBusinessStatusPlant(sysReportMaster.getTableName(),dataId.split(","), ZhiLiangConst.REPORT_STATUS_TEMP_REPORTTOTHECOURTFORAPPROVAL);
        }else if(BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.HOSPITALLEVEL){
            SysOrg currentTenOrg = extendSysOrgService.getTenOrg(ContextUtil.getCurrentUserId());
            if("W_QBD_QUALITY_ANALYSIS_REPORT".equals(sysReportMaster.getTableName())){
                String[] ids = dataId.split(",");
                for(int i=0;i<ids.length;i++){
                    WQbdQualityAnalysisReport wQbdQualityAnalysisReport = wQbdQualityAnalysisReportService.getOneById(ids[i]);
                    wQbdQualityAnalysisReport.setReportStatus(ZhiLiangConst.REPORT_STATUS_SUBMITTEDTOTHEGROUPFORAPPROVAL);
                    wQbdQualityAnalysisReport.setFilldeptHospital(currentTenOrg.getOrgName());
                    wQbdQualityAnalysisReport.setFilldeptHospitalId(currentTenOrg.getCode());
                    wQbdQualityAnalysisReport.setReportDate(new Date());
                    wQbdQualityAnalysisReportService.addAndupdateES(wQbdQualityAnalysisReport);
                }
            }
            //二级单位启动流程后修改状态为报集团审批中
            sysReportMasterDao.updateBusinessStatusHospital(sysReportMaster.getTableName(),dataId.split(","), ZhiLiangConst.REPORT_STATUS_SUBMITTEDTOTHEGROUPFORAPPROVAL);
            //修改业务表中填写部门院,填写部门院编码与上报日期字段
            sysReportMasterDao.updateBusinessColumnsHospitalByDataIds(sysReportMaster.getTableName(),dataId.split(","),currentTenOrg.getOrgName(),currentTenOrg.getCode(),new Date());
        }

        sysReportMasterDao.saveReportMaster(sysReportMaster);
        String[] dataArr = dataId.split(",");
        List<SysReportSub> sysReportSubList = new ArrayList<>();
        for (int i=0;i<dataArr.length;i++){
            SysReportSub sysReportSub = new SysReportSub();
            sysReportSub.setId(ProjectUUIDUtils.getUUIDFor32());
            sysReportSub.setRefId(sysReportMaster.getId());
            sysReportSub.setBusinessId(dataArr[i]);
            sysReportSub.setBusinessStatus("1");

            sysReportSubList.add(sysReportSub);
        }
        sysReportSubService.saveSysReportSub(sysReportSubList);
    }

    /**
     * 根据主键获取className
     * @param id
     * @return
     */
    public String getClassNameById(String id){
        return sysReportMasterDao.getClassNameById(id);
    }
}
